package nyist.media.service.impl;

import nyist.media.entity.ApplyRecord;
import nyist.media.entity.admin.Admin;
import nyist.media.enums.ApplyStatus;
import nyist.media.exception.BusinessException;
import nyist.media.page.ApiResult;
import nyist.media.page.AppPage;
import nyist.media.repository.ApplyRecordRepository;
import nyist.media.service.ApplyRecordService;
import nyist.media.vo.ApplyRecordVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 提交的申请单
 *
 * @author dcf
 * @date 2019/3/18
 */

@Service
public class ApplyRecordServiceImpl implements ApplyRecordService {

    @Autowired
    private ApplyRecordRepository applyRecordRepository;

    @Override
    public AppPage<ApplyRecordVO> findPage(PageRequest pageRequest, ApplyStatus status) {
        ApplyRecord applyRecord = new ApplyRecord();
        applyRecord.setStatus(status);
        return returnData(Example.of(applyRecord), pageRequest);
    }

    @Override
    public void auditApplyRecord(Long applyRecordId, Long adminId) {
        ApplyRecord record = applyRecordRepository.findOne(applyRecordId);
        if (Objects.isNull(record)) {
            throw new BusinessException(ApiResult.DATA_NOT_FOUND, "申请单不存在");
        }
        if (!record.getStatus().equals(ApplyStatus.APPLYING)) {
            throw new BusinessException(ApiResult.REJECTED_APPLY, "不能重复审核");
        } else {
            record.setStatus(ApplyStatus.APPLY_PASSED);
            record.setModifyDate(new Date());
            applyRecordRepository.save(record);
        }
    }

    @Override
    public void rejectedApplyRecord(Long id) {
        ApplyRecord applyRecord = applyRecordRepository.findOne(id);
        if (applyRecord.getStatus().equals(ApplyStatus.APPLYING)) {
            applyRecord.setStatus(ApplyStatus.APPLY_FAIL);
            applyRecord.setModifyDate(new Date());
            applyRecordRepository.save(applyRecord);
            throw new BusinessException(ApiResult.REJECTED_APPLY, "申请单不符合要求，已被驳回");
        } else {
            throw new BusinessException(ApiResult.REJECTED_APPLY);
        }
    }

    @Override
    public AppPage<ApplyRecordVO> findMyPage(PageRequest pageRequest, Admin currentInfo) {
        ApplyRecord applyRecord = new ApplyRecord();
        applyRecord.setAdmin(currentInfo);
        return returnData(Example.of(applyRecord), pageRequest);
    }

    @Override
    public void deleteOne(Long id) {
        applyRecordRepository.delete(id);
    }

    /**
     * 返回结果数据
     * @param example 查询条件对象
     * @param pageRequest 分页对象
     * @return 申请单数据
     */
    private AppPage<ApplyRecordVO> returnData(Example<ApplyRecord> example, PageRequest pageRequest) {
        Page<ApplyRecord> page = applyRecordRepository.findAll(example, pageRequest);
        List<ApplyRecordVO> applyRecordVOS = page.getContent().stream().map(ApplyRecordVO::new).collect(Collectors.toList());
        return new AppPage<>(pageRequest, page.getTotalElements(), page.getTotalPages(), applyRecordVOS);
    }


}
